package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.business.constant.StoreExtendConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreOnSaleSkuDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import com.ittx.dzt.tiktok.util.TiktokUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品下架
 * <p>
 * cy.sku.delisting
 * /product/202309/products/deactivate （禁用商品） https://partner.tiktokshop.com/docv2/page/6509de450fcef602bf1d087c?external_id=6509de450fcef602bf1d087c
 * </p>
 *
 * @author gywang
 * @since 2024-07-15
 */
@Service
public class TiktokSkuDeListingService extends AbstractBaseTiktokApiHandler<StoreOnSaleSkuDTO> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.PRODUCT_SETOFFLINE;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.SKU_DELISTING;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, StoreOnSaleSkuDTO storeOnSaleSkuDTO) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        // 商品id
        Map<String, Object> reqBody = new HashMap<>();
        String[] array = storeOnSaleSkuDTO.getPlatformSpuId().split(",");
        reqBody.put("product_ids", Arrays.asList(array));
        Result<?> result = this.executeTiktokApi(getDefaultUrl(platformApiParamDTO.getStoreConfig()), platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
        return result;
    }
}
